package com.daoos.ip;

import java.io.IOException;

/**
 * ip地址定位
 * Created by daoos on 17-2-10.
 */
public class Ip2RegionUtil {

    /**
     * Ip 2 region string.
     * 根据IP地址定位位置
     * @param ipStr the ip str
     * @return the string
     */
    public static String  ip2Region(String ipStr){
        String region=null;
        try {
            DbConfig config = new DbConfig();
            DbSearcher seacher = new DbSearcher(config, "./data/ip2region.db");
            double sTime = 0, cTime = 0;
            sTime = System.nanoTime();
            DataBlock  dataBlock =  seacher.binarySearch(ipStr) ;
            cTime = (System.nanoTime() - sTime) / 1000000;
            region=dataBlock.getRegion();
            System.out.printf("%s in %.5f millseconds\n", region, cTime);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DbMakerConfigException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            return region;
        }
    }
}
